<html lang="en">
	<head>
		<title>three.js webgpu - procedural texture</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Procedural Texture</span>
			</div>

			<small>
				GPU procedural texture generation.
			</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';
			import { checker, uv, uniform, convertToTexture } from 'three/tsl';
			import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js';

			import { Inspector } from 'three/addons/inspector/Inspector.js';

			let camera, scene, renderer;

			init();

			function init() {

				const aspect = window.innerWidth / window.innerHeight;
				camera = new THREE.OrthographicCamera( - aspect, aspect, 1, - 1, 0, 2 );
				camera.position.z = 1;

				scene = new THREE.Scene();

				// procedural to texture

				const uvScale = uniform( 4 );
				const blurAmount = uniform( .5 );

				const procedural = checker( uv().mul( uvScale ) );
				const proceduralToTexture = convertToTexture( procedural, 512, 512 ); // ( node, width, height )

				const colorNode = gaussianBlur( proceduralToTexture, blurAmount, 20 );

				// extra

				//proceduralToTexture.autoUpdate = false; // update just once
				//proceduralToTexture.textureNeedsUpdate = true; // manually update

				// scene

				const material = new THREE.MeshBasicNodeMaterial();
				material.colorNode = colorNode;

				const plane = new THREE.Mesh( new THREE.PlaneGeometry( 1, 1 ), material );
				scene.add( plane );

				// renderer

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setAnimationLoop( render );
				renderer.inspector = new Inspector();
				document.body.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

				// gui

				const gui = renderer.inspector.createParameters( 'Procedural Texture' );
				gui.add( uvScale, 'value', 1, 10 ).name( 'uv scale ( before rtt )' );
				gui.add( blurAmount, 'value', 0, 2 ).name( 'blur amount ( after rtt )' );
				gui.add( proceduralToTexture, 'autoUpdate' ).name( 'auto update' );

			}

			function onWindowResize() {

				renderer.setSize( window.innerWidth, window.innerHeight );

				const aspect = window.innerWidth / window.innerHeight;

				const frustumHeight = camera.top - camera.bottom;

				camera.left = - frustumHeight * aspect / 2;
				camera.right = frustumHeight * aspect / 2;

				camera.updateProjectionMatrix();

			}

			function render() {

				renderer.render( scene, camera );

			}

		</script>
	</body>
</html>
